from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired,ValidationError, DataRequired, Length
from app.models import User

class EditProfileForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired()])
    about_me = TextAreaField('自我介绍', validators=[Length(min=0, max=140)])
    submit = SubmitField('提交')
    
    def __init__(self, original_username, *args, **kwargs):
        super(EditProfileForm, self).__init__(*args, **kwargs)
        self.original_username = original_username
    
    def validate_username(self, username):
        if username.data != self.original_username:
            user = User.query.filter_by(username=self.username.data).first()
            if user is not None:
                raise ValidationError('用户名已使用')
            

class PostForm(FlaskForm):
    post = TextAreaField('发表', validators=[DataRequired(), Length(min=1, max=140)])
    submit = SubmitField('提交')
    

class EditPostForm(FlaskForm):
    body = TextAreaField('帖子内容', validators=[Length(min=0, max=140)])
    submit = SubmitField('提交')
    

class EmptyForm(FlaskForm):
    submit = SubmitField('提交')
    
class MessageForm(FlaskForm):
    message = TextAreaField('消息', validators=[DataRequired(), Length(min=0, max=140)])
    submit = SubmitField('提交')